using MicroMsgBackground.Common.Utils;
using MicroMsgBackground.Manager;
using MicroMsgBackground.Network;
using MicroMsgBackground.Scene;
using Microsoft.Phone.Info;
using Microsoft.Phone.Scheduler;
using System;
using System.Diagnostics;
using System.Windows;
namespace ScheduledTaskAgentMicroMsg
{
	public class ScheduledAgent : ScheduledTaskAgent
	{
		private const string TAG = "ScheduledAgent";
		private static volatile bool _classInitialized;
		public ScheduledAgent()
		{
			if (!ScheduledAgent._classInitialized)
			{
				ScheduledAgent._classInitialized = true;
				Deployment.get_Current().get_Dispatcher().BeginInvoke(delegate
				{
					Application.get_Current().add_UnhandledException(new EventHandler<ApplicationUnhandledExceptionEventArgs>(this.ScheduledAgent_UnhandledException));
				});
			}
		}
		private void ScheduledAgent_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
		{
			if (Debugger.get_IsAttached())
			{
				Debugger.Break();
			}
		}
		protected override void OnInvoke(ScheduledTask task)
		{
			RecycleMgr.setCurrentTick();
			if (!(task is PeriodicTask))
			{
				base.NotifyComplete();
				return;
			}
			Log.smethod_5("ScheduledAgent", string.Concat(new object[]
			{
				"OnInvoke begin MM:MemUse=",
				DeviceStatus.get_ApplicationPeakMemoryUsage(),
				"/",
				DeviceStatus.get_ApplicationMemoryUsageLimit()
			}));
			if (!AccountMgr.loadAccount())
			{
				Log.LogToFile();
				base.NotifyComplete();
				return;
			}
			RecycleMgr.loadRecyleInfo();
			if (RecycleMgr.needRecyle() && RecycleMgr.isInRecycleTime())
			{
				Log.smethod_5("ScheduledAgent", "doFileRecycle in InRecycleTime");
				RecycleMgr.doFileRecycle();
				Log.smethod_5("ScheduledAgent", string.Concat(new object[]
				{
					"OnInvoke end MM:MemUse=",
					DeviceStatus.get_ApplicationPeakMemoryUsage(),
					"/",
					DeviceStatus.get_ApplicationMemoryUsageLimit()
				}));
				Log.LogToFile();
				base.NotifyComplete();
				return;
			}
			AccountBackground curAccount = AccountMgr.getCurAccount();
			if (string.IsNullOrEmpty(curAccount.strPwd))
			{
				Log.smethod_5("ScheduledAgent", "strPwd is null or empty");
				Log.LogToFile();
				base.NotifyComplete();
				return;
			}
			SessionPackMgr.getAccount().setAuthInfo(curAccount.strUsrName, curAccount.strPwd, curAccount.strPwd2);
			SessionPackMgr.getAccount().setUin((int)curAccount.nUin);
			SessionPackMgr.setSessionKey(curAccount.bytesSessionkey);
			curAccount.nSyncTimes += 1u;
			AccountMgr.saveBackgroundAccout(curAccount);
			new NetSceneNewSync().doSceneBackground(7, syncScene.MM_NEWSYNC_SCENE_OTHER);
			Log.LogToFile();
			NetHandler.smethod_1(200);
			Log.smethod_5("ScheduledAgent", string.Concat(new object[]
			{
				"OnInvoke end MM:MemUse=",
				DeviceStatus.get_ApplicationPeakMemoryUsage(),
				"/",
				DeviceStatus.get_ApplicationMemoryUsageLimit()
			}));
			Log.LogToFile();
			base.NotifyComplete();
		}
	}
}
